import { mapState } from 'vuex'
import { 
  formatDateTime,
  getMinutesFromTime
} from '../utils/timeCalculator'

export default {
  computed: {
    ...mapState({
      workSettings: state => state.workSettings
    }),
    
    isWorkday() {
      const now = new Date()
      const today = now.getDay() || 7
      return this.workSettings.workdays.includes(today)
    },

    isWorking() {
      if (!this.workSettings) return false
      
      const now = new Date()
      const currentTime = now.getHours() * 60 + now.getMinutes()
      const morningStart = this.getMinutesFromTime(this.workSettings.morningStart)
      const morningEnd = this.getMinutesFromTime(this.workSettings.morningEnd)
      const afternoonStart = this.getMinutesFromTime(this.workSettings.afternoonStart)
      const afternoonEnd = this.getMinutesFromTime(this.workSettings.afternoonEnd)

      return (currentTime >= morningStart && currentTime <= morningEnd) ||
             (currentTime >= afternoonStart && currentTime <= afternoonEnd)
    },

    currentPeriod() {
      if (!this.isWorkday || !this.isWorking) return ''
      
      const now = new Date()
      const currentTime = now.getHours() * 60 + now.getMinutes()
      const morningStart = this.getMinutesFromTime(this.workSettings.morningStart)
      const morningEnd = this.getMinutesFromTime(this.workSettings.morningEnd)
      
      return currentTime >= morningStart && currentTime <= morningEnd ? '上午工作时段' : '下午工作时段'
    }
  },

  methods: {
    getMinutesFromTime(timeString) {
      if (!timeString) return 0
      const [hours, minutes] = timeString.split(':').map(Number)
      return hours * 60 + minutes
    },

    getWorkHours() {
      if (!this.workSettings) return 0
      
      const morningStart = this.getMinutesFromTime(this.workSettings.morningStart)
      const morningEnd = this.getMinutesFromTime(this.workSettings.morningEnd)
      const afternoonStart = this.getMinutesFromTime(this.workSettings.afternoonStart)
      const afternoonEnd = this.getMinutesFromTime(this.workSettings.afternoonEnd)
      
      const morningHours = (morningEnd - morningStart) / 60
      const afternoonHours = (afternoonEnd - afternoonStart) / 60
      
      return morningHours + afternoonHours
    },

    getCurrentWorkPeriod() {
      if (!this.workSettings) return 'morning'
      
      const now = new Date()
      const currentTime = now.getHours() * 60 + now.getMinutes()
      const morningStart = this.getMinutesFromTime(this.workSettings.morningStart)
      const morningEnd = this.getMinutesFromTime(this.workSettings.morningEnd)
      
      return currentTime >= morningStart && currentTime <= morningEnd ? 'morning' : 'afternoon'
    },

    formatDateTime(date) {
      return formatDateTime(date)
    }
  }
} 